package com.milkshopbackend.milkshop.controller;

import com.milkshopbackend.milkshop.Service.CategoryService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.CategoryDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    @GetMapping
    public Result<List<CategoryDTO>> list() {
        return Result.success(categoryService.listWithTree());
    }

    @GetMapping("/{id}")
    public Result<CategoryDTO> getById(@PathVariable Long id) {
        return Result.success(categoryService.getById(id));
    }

    @PostMapping
    public Result<Void> create(@RequestBody @Valid CategoryDTO categoryDTO) {
        categoryService.create(categoryDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody @Valid CategoryDTO categoryDTO) {
        categoryService.update(id, categoryDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        categoryService.delete(id);
        return Result.success();
    }
}